import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
 
/**
 * Caching to file.
 * You have a file of weights. Every person has a weight
 * in kilograms.
 * All the names are unique (there are no duplicate names).
 * A name is connected to the weight. Example of file:
 * <pre>
 * mati 100
 * kati 70
 * </pre>
 * 
 * This means that mati has a weight of 100 and 
 * kati has a weight of 70. If there are names which
 * have the same weight, those should be grouped together
 * in one line:
 * <pre>
 * mati juri 100
 * kati kerli 70
 * </pre>
 * 
 * The general format of the file is:
 * <pre>
 * name1 [name2] [name3] weight1
 * name4 [name5] [name6] weight2
 * </pre>
 * 
 * Every line has at least one name and weight.
 * A line can consist of additional names
 * (all names come before weight). Names consist
 * only of characters (not numbers, spaces, commas etc.).
 * 
 * @author Ago
 *
 */
public class EX06 {
	
	private static Map<Integer,List<String>> people
		= new HashMap<Integer,List<String>>();
 
	/**
	 * The main part.
	 * In this exercise, the main code is provided.
	 * You can modify this code as this method is not tested.
	 * 
	 * @param args Command line arguments
	 * @throws IOException Exception which can happen during input reading
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader reader = new BufferedReader(
				new InputStreamReader(System.in));
 
		String filename = "data/weights.txt";
		// the program should also work with non-existing file.
		// in that case there are no name-weight pairs
		readWeights(filename);
		//System.out.println(getWeight);
		while (true) {
			System.out.println("Name:");
			String name = reader.readLine();
			if (name == null || name.equals("")) {
				writeWeights(filename);
				System.out.println("exiting");
				break;
			}
			if (name.contains(" ")) {
				System.out.println("no spaces allowed!");
				continue;
			}
			Integer weight = getWeight(name);
			if (weight == null) {
				System.out.println("Person does not exist in DB.");
				while (true) {
					System.out.println("Weight:");
					String weightLine = reader.readLine();
					if (weightLine != null) {
						try {
							weight = Integer.parseInt(weightLine);
							if (weight > 10 && weight < 200) {
								setWeight(name, weight);
								break;
							}
							// note that this is not the requirement for other methods
							// it's just here to add some filtering example
 
							// note2: there is no "else" sentence here.
							// but as within if-block, "break" breaks out from while,
							// we get here only if if-block is not executed.
							// so, we could have this part also in "else" part.
							System.out.println("weight should be > 10 and < 200");
						} catch (NumberFormatException e) {
							System.out.println("not a number");
						}
					}
 
				} // end of while (true) - asking the weight
 
			} else {
				System.out.println(name + " weighs " + weight);
			}
		} // end of while (true) - asking names
		reader.close();
	}
 
	/**
	 * Sets the weight of the person with the given name.
	 * The weight should be stored in a class variable.
	 * This method should not try write anything to the file.
	 * @param name The name of the person
	 * @param weight The weight of the person
	 */
	public static void setWeight(String name, Integer weight) {
		// TODO: you should store name and weight somehow
		// tip: HashMap is probably a good idea
	}
 
	/**
	 * Opens a file with the given name and reads all
	 * the data from it. If the file does not exist,
	 * the method should just catch that error and
	 * no name-weight pairs will be stored.
	 * @param filename
	 */
	public static void readWeights(String filename) {
		byte[] bytes;
		try {
			bytes = Files.readAllBytes(Paths.get(filename));
			String fileContents = new String(bytes);
			parseWeights(fileContents);
		} catch (IOException e) {
			System.out.println("I/O Exception");
		}
		
		
 
 
		
	}
 
	/**
	 * Parses names and weights from the given string.
	 * This method is usually called by readWeights(String).
	 * <p>
	 * This method should not read anything from the file.
	 * All the information needed for this method is provided
	 * as a String argument.
	 * <p>
	 * The information should be stored in a class variable,
	 * which might be for example HashMap.
	 * 
	 * @param contents File contents to parse
	 */
	public static void parseWeights(String contents) {
		System.out.println(contents);
		Scanner s = new Scanner(contents);
		List<String> names = new ArrayList<String>();
		while(s.hasNext()){
			if(s.hasNextInt()){
				int weight = s.nextInt();
				people.put(weight, names);
				names.clear();
				
			}
			else{
				names.add(s.next());
			}
		}
		
		s.close();
		// TODO: implement
		// here you have to split the contents into
		// names and weights and store them in a 
		// class variable for later use.
	}
 
	/**
	 * Writes weight information from a class variable
	 * into file.
	 * @param filename The filename to be written. 
	 * 		Notice! the file will be overwriten.
	 */
	public static void writeWeights(String filename) {
		// TODO: implement
		// open the file
		// write information into file
		// close the file
	}
 
	/**
	 * Given the person name, the method should return the person's weight.
	 * If the weight does not exist (in a class variable, which also means 
	 * that it does not exist in file), NULL is returned.
	 * @param name Person name
	 * @return Weight if the person name exists in the cache (file),
	 * 			NULL otherwise.
	 */
	public static Integer getWeight(String name) {
		for(Integer key : people.keySet()){
			List<String> names = people.get(key);
			if(names.contains(name))
				return key;
			
		}
		// name exists?
		// if yes, return the weight
		// if no, return null
		return null;
	}
}